package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.OperateLog;
import model.User;

public class OperateLogService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tol.id,tol.createTime,tu.name AS userName,tol.name,tol.actionName,tol.params,tol.ip " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_operate_log tol ") ;
	    pageSql.join(" t_user tu " , " tu.id = tol.userId ") ;
		pageSql.setLikeCondition("tol.actionName") ; //请求路径	 
		pageSql.setLikeCondition("tol.name") ; //操作菜单	 
		pageSql.setLikeCondition("tu.name") ; //用户名	 
	    pageSql.orderBy(" tol.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}
	
	public void save(Record menu , String actionName , String params , User user , String ip) {
		OperateLog log = new OperateLog();
		log.setActionName(actionName);
		log.setParams(params);
		log.setUserId(user.getId());
		String name = StrKit.isBlank(menu.getStr("parentName")) ? 
				menu.getStr("name") : menu.getStr("parentName") + " > " + menu.getStr("name") ;
		log.setName(name);
		log.setIp(ip);
		log.save();
	}
}
